package com.hnkywl.sts.dao.xsgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.xsgl.Gd;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.*;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-23
 * Time: 下午2:21
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class GdDao extends SimpleHibernateTemplate<Gd, Long> {

    //根据code查询
    public Gd findGdByCode(String resource_serial_number) {
        String hql = "from Gd where resource_serial_number = ?";
        List<Gd> gdList = this.find(hql, resource_serial_number);
        if (gdList != null && gdList.size() > 0)
            return gdList.get(0);
        return null;
    }

    //根据挂牌号查询
    public Gd findGdByHangCode(String hangcode) {
        return findUniqueByProperty("hangcode", hangcode);
    }

    //================================撤单===============================================
    public Page<Gd> findCancellationList(String khbm, String ckmc, String code, String ggxh, String wzmc, int pageNo, int pageSize) {
        Page<Gd> page = new Page<Gd>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");

        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("kh.jtwz", "jtwz", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh", "kh", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.gys", "gys", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.sbgs", "sbgs", CriteriaSpecification.LEFT_JOIN)
                .createAlias("kh.cggs", "cggs", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                                .add(Property.forName("id").as("id"))
                                .add(Property.forName("kh.id").as("kh.id"))
                                .add(Property.forName("kh.khbm").as("khbm"))
                                .add(Property.forName("cggs.qc").as("cggs_qc"))
                                .add(Property.forName("sbgs.qc").as("sbgs_qc"))
                                .add(Property.forName("gys.qc").as("gys_qc"))
                                .add(Projections.sqlProjection("jtwz1_.code as code", new String[]{"code"}, new Type[]{Hibernate.STRING}))
                                .add(Projections.sqlProjection("jtwz1_.name as name", new String[]{"name"}, new Type[]{Hibernate.STRING}))
                                .add(Projections.sqlProjection("jtwz1_.ggxh as ggxh", new String[]{"ggxh"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("jtwz.dw").as("dw"))
                                .add(Property.forName("cd.z").as("cd"))
                                .add(Property.forName("resource_serial_number").as("resource_serial_number"))
                                .add(Projections.sqlProjection("hangcode as hangcode", new String[]{"hangcode"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("stander_name").as("stander_name"))
                                .add(Property.forName("cate").as("cate"))
                                .add(Property.forName("material").as("material"))
                                .add(Property.forName("spec").as("spec"))
                                .add(Property.forName("brand").as("brand"))
                                .add(Property.forName("measure_count").as("measure_count"))
                                .add(Property.forName("total_measure").as("total_measure"))
                                .add(Property.forName("market_price").as("market_price"))
                                .add(Property.forName("measure").as("measure"))
                                .add(Property.forName("price").as("price"))
                                .add(Property.forName("preferential_price").as("preferential_price"))
                                .add(Property.forName("user_id").as("user_id"))
                                .add(Property.forName("production_date").as("production_date"))
                                .add(Projections.sqlProjection("warehouse as warehouse", new String[]{"warehouse"}, new Type[]{Hibernate.STRING}))
                                .add(Property.forName("lastUpdated"))
                )
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        if (StringUtils.isNotEmpty(khbm)) {
            criteria.add(Property.forName("kh.khbm").like("%" + khbm.trim() + "%", MatchMode.ANYWHERE));
        }
        if (StringUtils.isNotEmpty(ckmc)) {
            criteria.add(Property.forName("warehouse").like("%" + ckmc.trim() + "%", MatchMode.ANYWHERE));
        }
        if (StringUtils.isNotEmpty(code)) {
            criteria.add(Property.forName("jtwz.code").like("%" + code.trim() + "%", MatchMode.ANYWHERE));
        }
        if (StringUtils.isNotEmpty(ggxh)) {
            criteria.add(Property.forName("jtwz.ggxh").like("%" + ggxh.trim() + "%", MatchMode.ANYWHERE));
        }
        if (StringUtils.isNotEmpty(wzmc)) {
            criteria.add(Property.forName("jtwz.name").like("%" + wzmc.trim() + "%", MatchMode.ANYWHERE));
        }
        criteria.add(Restrictions.isNotNull("hangcode"));
        RegionDao.createDataRegion(criteria);
        Page list = this.findByCriteria(page, criteria);
        return list;
    }

    /**
     * 验证材料号唯一性
     *
     * @param clh
     * @return
     */
    public Boolean findUniqueByClh(String clh) {
        String sql = "SELECT COUNT(*) FROM t_biz_gd t WHERE t.matno = ?";
        int i = Integer.parseInt(createSQLQuery(sql, clh).uniqueResult().toString());
        if (i > 0) {
            return true;
        }
        return false;
    }
}
